package com.huanzhidadi.single.atomicinteger;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerTest {

    private static AtomicInteger atomicInteger = new AtomicInteger();

    private static void increase() {
        atomicInteger.incrementAndGet();
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 10000; j++) {
                    increase();
                }
            }).start();
        }
        // 所有线程累加完成后输出 如果除了主线程外还有子线程存在，则主线程让出执行时间
        while (Thread.activeCount() > 2) Thread.yield();
        System.out.println(atomicInteger);
    }
}
